package org.gdstash.ui.select;

import org.gdstash.db.SelectionCriteria;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class WeaponTypePane extends AdjustablePanel {
    private JCheckBox cbAxe1H;
    private JCheckBox cbAxe2H;
    private JCheckBox cbMace1H;
    private JCheckBox cbMace2H;
    private JCheckBox cbSpear1H;
    private JCheckBox cbSpear2H;
    private JCheckBox cbSword1H;
    private JCheckBox cbSword2H;
    private JCheckBox cbDagger1H;
    private JCheckBox cbScepter1H;
    private JCheckBox cbRanged1H;
    private JCheckBox cbRanged2H;
    private JCheckBox cbStaff2H;
    private JCheckBox cbShield;
    private JCheckBox cbOffhand;

    public WeaponTypePane() {
        this(1);
    }

    public WeaponTypePane(final int direction) {
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbAxe1H).addComponent(this.cbDagger1H).addComponent(this.cbMace1H).addComponent(this.cbScepter1H).addComponent(this.cbSpear1H).addComponent(this.cbSword1H).addComponent(this.cbAxe2H).addComponent(this.cbMace2H).addComponent(this.cbSpear2H).addComponent(this.cbSword2H).addComponent(this.cbStaff2H).addComponent(this.cbRanged1H).addComponent(this.cbRanged2H).addComponent(this.cbShield).addComponent(this.cbOffhand));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbAxe1H)).addGroup(layout.createParallelGroup().addComponent(this.cbDagger1H)).addGroup(layout.createParallelGroup().addComponent(this.cbMace1H)).addGroup(layout.createParallelGroup().addComponent(this.cbScepter1H)).addGroup(layout.createParallelGroup().addComponent(this.cbSpear1H)).addGroup(layout.createParallelGroup().addComponent(this.cbSword1H)).addGroup(layout.createParallelGroup().addComponent(this.cbAxe2H)).addGroup(layout.createParallelGroup().addComponent(this.cbMace2H)).addGroup(layout.createParallelGroup().addComponent(this.cbSpear2H)).addGroup(layout.createParallelGroup().addComponent(this.cbSword2H)).addGroup(layout.createParallelGroup().addComponent(this.cbStaff2H)).addGroup(layout.createParallelGroup().addComponent(this.cbRanged1H)).addGroup(layout.createParallelGroup().addComponent(this.cbRanged2H)).addGroup(layout.createParallelGroup().addComponent(this.cbShield)).addGroup(layout.createParallelGroup().addComponent(this.cbOffhand));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        } else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.cbAxe1H, this.cbDagger1H);
        layout.linkSize(0, this.cbAxe1H, this.cbMace1H);
        layout.linkSize(0, this.cbAxe1H, this.cbScepter1H);
        layout.linkSize(0, this.cbAxe1H, this.cbSpear1H);
        layout.linkSize(0, this.cbAxe1H, this.cbSword1H);
        layout.linkSize(0, this.cbAxe1H, this.cbAxe2H);
        layout.linkSize(0, this.cbAxe1H, this.cbMace2H);
        layout.linkSize(0, this.cbAxe1H, this.cbSpear2H);
        layout.linkSize(0, this.cbAxe1H, this.cbSword2H);
        layout.linkSize(0, this.cbAxe1H, this.cbRanged1H);
        layout.linkSize(0, this.cbAxe1H, this.cbRanged2H);
        layout.linkSize(0, this.cbAxe1H, this.cbStaff2H);
        layout.linkSize(0, this.cbAxe1H, this.cbShield);
        layout.linkSize(0, this.cbAxe1H, this.cbOffhand);
        layout.linkSize(1, this.cbAxe1H, this.cbDagger1H);
        layout.linkSize(1, this.cbAxe1H, this.cbMace1H);
        layout.linkSize(1, this.cbAxe1H, this.cbScepter1H);
        layout.linkSize(1, this.cbAxe1H, this.cbSpear1H);
        layout.linkSize(1, this.cbAxe1H, this.cbSword1H);
        layout.linkSize(1, this.cbAxe1H, this.cbAxe2H);
        layout.linkSize(1, this.cbAxe1H, this.cbMace2H);
        layout.linkSize(1, this.cbAxe1H, this.cbSpear2H);
        layout.linkSize(1, this.cbAxe1H, this.cbSword2H);
        layout.linkSize(1, this.cbAxe1H, this.cbRanged1H);
        layout.linkSize(1, this.cbAxe1H, this.cbRanged2H);
        layout.linkSize(1, this.cbAxe1H, this.cbStaff2H);
        layout.linkSize(1, this.cbAxe1H, this.cbShield);
        layout.linkSize(1, this.cbAxe1H, this.cbOffhand);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCheck = UIManager.getDefaults().getFont("CheckBox.font");
        if (fntCheck == null) {
            fntCheck = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCheck = fntCheck.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_TYPE_WEAPON"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbAxe1H == null) {
            this.cbAxe1H = new JCheckBox();
        }
        this.cbAxe1H.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TYPE_WEAPON_AXE"));
        this.cbAxe1H.setFont(fntCheck);
        if (this.cbDagger1H == null) {
            this.cbDagger1H = new JCheckBox();
        }
        this.cbDagger1H.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TYPE_WEAPON_DAGGER"));
        this.cbDagger1H.setFont(fntCheck);
        if (this.cbMace1H == null) {
            this.cbMace1H = new JCheckBox();
        }
        this.cbMace1H.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TYPE_WEAPON_MACE"));
        this.cbMace1H.setFont(fntCheck);
        if (this.cbScepter1H == null) {
            this.cbScepter1H = new JCheckBox();
        }
        this.cbScepter1H.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TYPE_WEAPON_SCEPTER"));
        this.cbScepter1H.setFont(fntCheck);
        if (this.cbSpear1H == null) {
            this.cbSpear1H = new JCheckBox();
        }
        this.cbSpear1H.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TYPE_WEAPON_SPEAR"));
        this.cbSpear1H.setFont(fntCheck);
        if (this.cbSword1H == null) {
            this.cbSword1H = new JCheckBox();
        }
        this.cbSword1H.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TYPE_WEAPON_SWORD"));
        this.cbSword1H.setFont(fntCheck);
        if (this.cbAxe2H == null) {
            this.cbAxe2H = new JCheckBox();
        }
        this.cbAxe2H.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TYPE_WEAPON_AXE_2H"));
        this.cbAxe2H.setFont(fntCheck);
        if (this.cbMace2H == null) {
            this.cbMace2H = new JCheckBox();
        }
        this.cbMace2H.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TYPE_WEAPON_MACE_2H"));
        this.cbMace2H.setFont(fntCheck);
        if (this.cbSpear2H == null) {
            this.cbSpear2H = new JCheckBox();
        }
        this.cbSpear2H.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TYPE_WEAPON_SPEAR_2H"));
        this.cbSpear2H.setFont(fntCheck);
        if (this.cbStaff2H == null) {
            this.cbStaff2H = new JCheckBox();
        }
        this.cbStaff2H.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TYPE_WEAPON_STAFF_2H"));
        this.cbStaff2H.setFont(fntCheck);
        if (this.cbSword2H == null) {
            this.cbSword2H = new JCheckBox();
        }
        this.cbSword2H.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TYPE_WEAPON_SWORD_2H"));
        this.cbSword2H.setFont(fntCheck);
        if (this.cbRanged1H == null) {
            this.cbRanged1H = new JCheckBox();
        }
        this.cbRanged1H.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TYPE_WEAPON_RANGED"));
        this.cbRanged1H.setFont(fntCheck);
        if (this.cbRanged2H == null) {
            this.cbRanged2H = new JCheckBox();
        }
        this.cbRanged2H.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TYPE_WEAPON_RANGED_2H"));
        this.cbRanged2H.setFont(fntCheck);
        if (this.cbShield == null) {
            this.cbShield = new JCheckBox();
        }
        this.cbShield.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TYPE_WEAPON_SHIELD"));
        this.cbShield.setFont(fntCheck);
        if (this.cbOffhand == null) {
            this.cbOffhand = new JCheckBox();
        }
        this.cbOffhand.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TYPE_WEAPON_OFFHAND"));
        this.cbOffhand.setFont(fntCheck);
    }

    public void setSelection(final int sel) {
        this.clear();
        switch (sel) {
            case 1: {
                this.cbAxe1H.setSelected(true);
                this.cbAxe2H.setSelected(true);
                this.cbMace1H.setSelected(true);
                this.cbMace2H.setSelected(true);
                this.cbSpear1H.setSelected(true);
                this.cbSpear2H.setSelected(true);
                this.cbSword1H.setSelected(true);
                this.cbSword2H.setSelected(true);
                this.cbDagger1H.setSelected(true);
                this.cbScepter1H.setSelected(true);
                this.cbStaff2H.setSelected(true);
                this.cbRanged1H.setSelected(true);
                this.cbRanged2H.setSelected(true);
                this.cbShield.setSelected(true);
                this.cbOffhand.setSelected(true);
                break;
            }
            case 2: {
                this.cbAxe1H.setSelected(true);
                this.cbAxe2H.setSelected(true);
                this.cbMace1H.setSelected(true);
                this.cbMace2H.setSelected(true);
                this.cbSpear1H.setSelected(true);
                this.cbSpear2H.setSelected(true);
                this.cbSword1H.setSelected(true);
                this.cbSword2H.setSelected(true);
                this.cbDagger1H.setSelected(true);
                this.cbScepter1H.setSelected(true);
                this.cbStaff2H.setSelected(true);
                this.cbRanged1H.setSelected(true);
                this.cbRanged2H.setSelected(true);
                this.cbShield.setSelected(true);
                this.cbOffhand.setSelected(true);
            }
            case 3: {
            }
            case 4: {
            }
            case 6: {
                this.cbAxe1H.setSelected(true);
                this.cbAxe2H.setSelected(true);
                this.cbMace1H.setSelected(true);
                this.cbMace2H.setSelected(true);
                this.cbSpear1H.setSelected(true);
                this.cbSpear2H.setSelected(true);
                this.cbSword1H.setSelected(true);
                this.cbSword2H.setSelected(true);
                break;
            }
            case 7: {
                this.cbAxe1H.setSelected(true);
                this.cbMace1H.setSelected(true);
                this.cbSpear1H.setSelected(true);
                this.cbSword1H.setSelected(true);
                break;
            }
            case 8: {
                this.cbAxe2H.setSelected(true);
                this.cbMace2H.setSelected(true);
                this.cbSpear2H.setSelected(true);
                this.cbSword2H.setSelected(true);
                break;
            }
            case 9: {
                this.cbDagger1H.setSelected(true);
                this.cbScepter1H.setSelected(true);
                this.cbStaff2H.setSelected(true);
                break;
            }
            case 10: {
                this.cbRanged1H.setSelected(true);
                this.cbRanged2H.setSelected(true);
                this.cbStaff2H.setSelected(true);
                break;
            }
        }
    }

    public void clear() {
        this.cbAxe1H.setSelected(false);
        this.cbAxe2H.setSelected(false);
        this.cbMace1H.setSelected(false);
        this.cbMace2H.setSelected(false);
        this.cbSpear1H.setSelected(false);
        this.cbSpear2H.setSelected(false);
        this.cbSword1H.setSelected(false);
        this.cbSword2H.setSelected(false);
        this.cbDagger1H.setSelected(false);
        this.cbScepter1H.setSelected(false);
        this.cbStaff2H.setSelected(false);
        this.cbRanged1H.setSelected(false);
        this.cbRanged2H.setSelected(false);
        this.cbShield.setSelected(false);
        this.cbOffhand.setSelected(false);
    }

    public void addCriteria(final SelectionCriteria criteria) {
        if (this.cbAxe1H.isSelected()) {
            criteria.itemClass.add("WeaponMelee_Axe");
        }
        if (this.cbAxe2H.isSelected()) {
            criteria.itemClass.add("WeaponMelee_Axe2h");
        }
        if (this.cbMace1H.isSelected()) {
            criteria.itemClass.add("WeaponMelee_Mace");
        }
        if (this.cbMace2H.isSelected()) {
            criteria.itemClass.add("WeaponMelee_Mace2h");
        }
        if (this.cbSpear1H.isSelected()) {
            criteria.itemClass.add("WeaponHunting_Spear");
        }
        if (this.cbSpear2H.isSelected()) {
            criteria.itemClass.add("WeaponMelee_Spear2h");
        }
        if (this.cbSword1H.isSelected()) {
            criteria.itemClass.add("WeaponMelee_Sword");
        }
        if (this.cbSword2H.isSelected()) {
            criteria.itemClass.add("WeaponMelee_Sword2h");
        }
        if (this.cbDagger1H.isSelected()) {
            criteria.itemClass.add("WeaponMelee_Dagger");
        }
        if (this.cbScepter1H.isSelected()) {
            criteria.itemClass.add("WeaponMelee_Scepter");
        }
        if (this.cbStaff2H.isSelected()) {
            criteria.itemClass.add("WeaponMagical_Staff");
        }
        if (this.cbRanged1H.isSelected()) {
            criteria.itemClass.add("WeaponHunting_Ranged1h");
        }
        if (this.cbRanged2H.isSelected()) {
            criteria.itemClass.add("WeaponHunting_Ranged2h");
        }
        if (this.cbShield.isSelected()) {
            criteria.itemClass.add("WeaponArmor_Shield");
        }
        if (this.cbOffhand.isSelected()) {
            criteria.itemClass.add("WeaponArmor_Offhand");
        }
    }

    public void addComponentCriteria(final SelectionCriteria criteria) {
        criteria.slotAxe1H = this.cbAxe1H.isSelected();
        criteria.slotAxe2H = this.cbAxe2H.isSelected();
        criteria.slotMace1H = this.cbMace1H.isSelected();
        criteria.slotMace2H = this.cbMace2H.isSelected();
        criteria.slotSpear1H = this.cbSpear1H.isSelected();
        criteria.slotSpear2H = this.cbSpear2H.isSelected();
        criteria.slotSword1H = this.cbSword1H.isSelected();
        criteria.slotSword2H = this.cbSword2H.isSelected();
        criteria.slotDagger1H = this.cbDagger1H.isSelected();
        criteria.slotScepter1H = this.cbScepter1H.isSelected();
        criteria.slotStaff2H = this.cbStaff2H.isSelected();
        criteria.slotRanged1H = this.cbRanged1H.isSelected();
        criteria.slotRanged2H = this.cbRanged2H.isSelected();
        criteria.slotShield = this.cbShield.isSelected();
        criteria.slotOffhand = this.cbOffhand.isSelected();
    }
}
